package com.tai.cobrapp.activity.crud;

import java.util.ArrayList;
import java.util.List;

import com.tai.cobrapp.R;
import com.tai.cobrapp.activity.AdminPanelActivity;
import com.tai.cobrapp.activity.helpers.MenuActivity;
import com.tai.cobrapp.backend.Backend;
import com.tai.cobrapp.database.DatabaseHelper;
import com.tai.cobrapp.model.Dispositivo;
import com.tai.cobrapp.model.Establecimiento;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class NewDeviceActivity extends MenuActivity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_device);
		
		Spinner spinner = (Spinner) findViewById(R.id.establishment_spinner);
		// Create an ArrayAdapter using the string array and a default spinner layout
		//ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.hospitals_spinner, android.R.layout.simple_spinner_item);
		// PRUEBA con el adaptar creado en backend
		ArrayList<Establecimiento> listadoEstab = Backend.getInstance().getActiveEstablecimientos(null, this);
		
		//ArrayAdapter<Establecimiento> adapter = new ArrayAdapter<Establecimiento>(this, android.R.layout.simple_spinner_item, listadoEstab);
	    //adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    //spinner.setAdapter(adapter);
		
	    populateSpinner(this, listadoEstab, spinner);
		//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,listadoEstab ); 
				//new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, listadoEstab);
		// Specify the layout to use when the list of choices appears
		
		
		spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {
			@Override
		    public void onItemSelected(AdapterView<?> parent, View selectedItemView, int position, long id) {
				
				//Spinner spinner = (Spinner) parent;
				Toast.makeText(NewDeviceActivity.this, "ID: " + parent.getItemAtPosition(position).toString() , Toast.LENGTH_SHORT).show();
				
		    }


			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}

		});
		
	}
	@Override
	public void onBackPressed() {
		Intent i = new Intent(NewDeviceActivity.this, AdminPanelActivity.class);
		startActivity(i);
		super.onBackPressed();
	}
	public void finish_device_btn_pressed(View view){
		Intent i = new Intent(NewDeviceActivity.this, AdminPanelActivity.class);
		startActivity(i);
	}
	public void save_device_btn_pressed(View view){
		EditText nameDisp = (EditText)findViewById(R.id.device_name);
		EditText description = (EditText)findViewById(R.id.device_description);
		Spinner spin = (Spinner)findViewById(R.id.establishment_spinner);
		Integer idEstablecimiento = getIdEstablecimiento(spin.getSelectedItem().toString());
		if (idEstablecimiento == -1){
			Toast.makeText(this, R.string.error_assigning_establishment, Toast.LENGTH_SHORT).show();
		}else {
			if(nameDisp.getText().toString() == null || nameDisp.getText().toString().isEmpty()){
				Toast.makeText(this, R.string.fields_incomplete, Toast.LENGTH_LONG).show();
				return;
			}
			if(existeDevice(nameDisp.getText())){
				Toast.makeText(this, R.string.device_already_exists, Toast.LENGTH_SHORT).show();
				nameDisp.setText("");
			}
			else{
				Dispositivo dispositivo = new Dispositivo(idEstablecimiento, nameDisp.getText(), description.getText());
				insertDispositivo(dispositivo);
				Toast.makeText(this, R.string.device_creation_successful, Toast.LENGTH_SHORT).show();
				nameDisp.setText("");
				description.setText("");
			}
		}
	}
	public Integer getIdEstablecimiento(String name){
		return DatabaseHelper.getDatabaseInstance(this).getIdEstablecimiento(name);
	}
	
	public boolean existeDevice(Editable device){
		//return false;
		if(DatabaseHelper.getDatabaseInstance(this).existeDispositivo(device)){
			return true;
		}else{
			return false;
		}
	}
	public void insertDispositivo(Dispositivo disp){
		DatabaseHelper.getDatabaseInstance(this).insertarDispositivo(disp);
	}
	
	public void populateSpinner(Context context, List<Establecimiento> establecimientos, Spinner spinner)
	{
	    List<String> list = new ArrayList<String>();
	    for(Establecimiento dep : establecimientos) {
	        list.add(dep.getName().toString());
	    }

	    ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, list);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spinner.setAdapter(adapter);
	}   
}
